package io.renren.modules.job.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.Constant;
import io.renren.modules.pms.dao.GoodsStockRecordDao;
import io.renren.modules.pms.entity.GoodsStockRecordEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JinYang
 * @version 1.0
 * @belongsProject supermarket-management-system
 * @belongsPackage io.renren.task
 * @createTime 2024-08-14  19:05
 * @description 商品临期推送警告
 */
@Component
@Slf4j
public class GoodsTask {

    @Resource
    private GoodsStockRecordDao goodsStockRecordDao;

    /**
     * 处理商品临期告警
     */
    @Transactional
//    @Scheduled(cron = "0 0 0 * * ?")//  表示每天凌晨0点执行一次定时任务
    @Scheduled(cron = "0 0/1 * * * ?")//  测试频率 每分钟一次
    public void processOrderTask() {
//        先查未删除商品商品
        // 计算45天后的日期
        LocalDate currentDate = LocalDate.now();

        LocalDate thresholdDate = currentDate.plusDays(45);

        List<GoodsStockRecordEntity> goodsList = goodsStockRecordDao.selectList(
                new QueryWrapper<GoodsStockRecordEntity>()
                        .lt("date_due", thresholdDate)
                        .eq("delete_state", Constant.NOT_DELETE)
                        .eq("type", 0)
        );

//        MailUtil mailUtil = new MailUtil("2223041822@qq.com", "");

        List<Map<String, Object>> goodsRemnant = new ArrayList<>();

        goodsList.forEach(goods -> {
            Integer shelfLife = goods.getShelfLife(); //获取保质期天数
//            到产日期 - 当天日期 = 剩余保质期1
            Long days = ChronoUnit.DAYS.between(LocalDate.now(), goods.getDateDue().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
            log.info("商品剩余保质期:{}天", days);

            Integer tmp = 0; //临时标记 1表示该商品临近过期
            if (shelfLife >= Constant.GOODS_SHELF_LIFE_YEAR && days < 45) {
//                一年的商品，45天进行提醒
                tmp = 1;
            } else if (shelfLife >= Constant.GOODS_SHELF_LIFE_SEMESTER && days < 20) {
                tmp = 1;
            } else if (shelfLife >= Constant.GOODS_SHELF_LIFE_QUARTER && days < 15) {
                tmp = 1;
            } else if (shelfLife >= Constant.GOODS_SHELF_LIFE_MONTH && days < 10) {
                tmp = 1;
            } else if (shelfLife >= Constant.GOODS_SHELF_LIFE_WEEK && days < 5) {
                tmp = 1;
            } else if (shelfLife < Constant.GOODS_SHELF_LIFE_WEEK && days <= 3) {
                tmp = 1;
            }

//            该商品即将过期发送推文
            if (tmp == 1) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("商品名", goods.getGoodsName());
                map.put("批次", goods.getProductionBatch());
                map.put("剩余天数", days);
                goodsRemnant.add(map);
            }

        });


        log.info("临期名单:{}", goodsRemnant);
//        mailUtil.send("2223041822@qq.com","商品即将过期名单",goodsRemnant.toString());

    }

}
